package com.atguigu.guli.service.ucenter.controller.api;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.utils.JwtHelper;
import com.atguigu.guli.service.base.utils.JwtInfo;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.form.MemberRegisForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-07-11
 */
@Api(tags = "用户管理")
@RestController
@CrossOrigin
@RequestMapping("/api/ucenter/member")
public class ApiMemberController {

    @Autowired
    private MemberService memberService;

    // 注册用户
    @ApiOperation(value = "注册用户")
    @PostMapping("regist")
    public R regis(@RequestBody MemberRegisForm memberRegis) {

        memberService.saveMember(memberRegis);
        return R.ok();
    }

    // 用户登录
    @ApiOperation(value = "用户登录")
    @PostMapping("login")
    public R login(@RequestParam("mobile") String mobile,
                   @RequestParam("password") String password
    ) {
        String jwt = memberService.login(mobile, password);
        return R.ok().data("jwt", jwt);
    }

    // 获取用户登录后的信息, 头像, 昵称, id; 从GULI-TOKEN中获取 请求头
    @ApiOperation(value = "获取用户登录后的信息")
    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request) {

        // 如果请求报文的头中提交了token 值是jwt的字符串，那么 JwtHelper的工具方法可以帮助我们自动获取解析用户信息
        JwtInfo jwtInfo = JwtHelper.getJwtInfo(request);
        return R.ok().data("item", jwtInfo);
    }

}

